<?php

namespace ticky;

/**
 *  工厂类
 */
class db {

    public static $instance = null;
    public static $config = null;

    private static function getconfig() {
        self::$config = config::get('db');
    }

    /**
     * 返回当前终级类对象的实例
     * @return object
     */
    public static function instance($name = false) {
        self::getconfig();
        $type = !empty(self::$driver) ? self::$driver : 'db_pdo';
        if (false === $name) {
            $name = md5(serialize(self::$config));
        }
        if (true === $name || !isset(self::$instance[$name])) {
            $class = false === strpos($type, '\\') ? '\\ticky\\db\\driver\\' . $type : $type;
            if (true === $name) {
                return new $class(self::$config, $name);
            }
            self::$instance[$name] = new $class(self::$config, $name);
        }
        return self::$instance[$name];
    }

}
